OPC Studio User's Guide and Reference
PHP Examples
Client and Subscriber Development > Examples > Examples in the Product and on the Web > COM Examples > PHP Examples
In This Topic
For the very simplest, introductory examples, see this page on our main Web site: Integrating PHP and OPC.
Various PHP versions can be downloaded from http://php.net/downloads.php . PHP for Windows is available from https://windows.php.net/download/ .

In some earlier versions, the examples were tested in PHP v5.6, under command-line interpreter and Internet Information Server (IIS). In QuickOPC 2019.2, the examples were tested in PHP 7.3.10 (VC15 x64 Thread Safe).

The examples use the COM extension for PHP. This extension can be enabled e.g. in following ways:

a)      Add following to the php.ini file:

extension=ext/php_com_dotnet.dll

b)       Use following in the php.ini file:

extension_dir =  "ext"

extension=php_com_dotnet.dll

c)       From IIS, under Internet Information Services (IIS) Manager, by PHP Manager: Use

[PHP_COM_DOTNET]

extension=php_com_dotnet.dll

PHP Console Examples

In order to run the command-line examples, use

PHP.EXE –f “filename

For easier troubleshooting of the command-line scripts, it is recommended that you enable the display_error option, e.g. in one of the following ways:

a)      Use the following on the command line:

-d display_errors

b)      Add following to the php.ini file:

display_errors = On

c)       Copy the file php.ini-development over your php.ini file. 

 The provided examples are:

For those who want to use the command-line interface, a "Command Prompt From Here" link is installed into the examples folder. This link allows you to switch from the Windows Explorer to a CMD window with the current directory set to the chosen place, for quick experiments and exploration.

PHP Web Examples

In order to run the Web-based examples, you need to configure your Web server to logically expose the physical directory where the examples are installed.

Examples in this folder are Web server scripts: the page, including OPC data, is generated on the server. The EasyOPC component must be installed on the server side, and the OPC server(s) must be accessible locally or remotely from the computer where EasyOPC component resides.

IIS Web Server

In IIS, a virtual directory needs to be created to point to the physical directory with the examples.

Apache Web Server

We have used roughly following steps to test the PHP Web examples with Apache:

  1. Install WampServer from http://sourceforge.net/projects/wampserver/ .
  2. Start WampServer services.
  3. Using the tray menu icon, under Apache -> Alias directories, create a new alias named e.g. "UADocExamples", and pointed it to the physical directory where we have our Web PHP examples.
  4. Open the Web browser and enter http://UADocExamples/test/ReadValue.Web.php .

We have also tested with FastCGI, using the steps outlined here:  How to Configure FastCGI on WAMP... .

PHP Development Specifics

PHP cannot navigate between COM interfaces of an object, or provide an "amalgamated" set of members from multiple interfaces, when a specific interface is returned by a property or method. This leads to a problem if you want to downcast the object you have to a derived type.

For example, if you work with OPC UA Complex Data Extension, you may receive a UAGenericObject containing GenericData Object. PHP will allow you to access members declared on the GenericData Object, but that may not be enough. There are various derivations of the GenericData, such as the StructuredData. If you have received StructuredData, PHP will not give you any way to access the new members it has over the GenericData. In this particular case, we have provided methods on the GenericData Object such as AsStructuredData, which allow you to cast the received data appropriately.

It is not, however, possible to provide such methods in all situations and for object types. In such case, you can use the InteropHelper Object and its AsObject Method. By passing the object through this method, PHP is forced to use late binding, allowing the intended members be accessed.

See Also